package com.yanxibao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanxibao.bean.UmInfo;
import com.yanxibao.common.Result;
import com.yanxibao.dto.PageQueryDTO;
import com.yanxibao.mapper.UmInfoMapper;
import com.yanxibao.service.UmInfoService;
import com.yanxibao.util.QueryUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 院校专业详情;(_umInfo)表服务实现类
 *
 * @author kongke
 * @date 2023/11/16
 */
@Service
public class UmInfoServiceImpl extends ServiceImpl<UmInfoMapper, UmInfo> implements UmInfoService {

    @Resource
    private UmInfoMapper umInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param umInfoId 主键
     * @return 实例对象
     */
    @Override
    public Result<UmInfo> queryById(Integer umInfoId) {
        UmInfo umInfo = getById(umInfoId);
        return umInfo != null ? Result.success(umInfo) : Result.error("07000", "未查询到信息");

    }

    /**
     * 分页查询
     *
     * @param umInfo 筛选条件
     */
    @Override
    public Result<PageQueryDTO<UmInfo>> pageQuery(UmInfo umInfo, Integer current, Integer size) {

        Page<UmInfo> page = Page.of(current, size);

        QueryWrapper<UmInfo> wrapper = new QueryWrapper<>();
        Integer time = umInfo.getUminfoTime();

        if (time != null) {
            wrapper.eq("uminfo_time", time);
            umInfo.setUminfoTime(null);
        }

        @SuppressWarnings("unchecked")
        PageQueryDTO<UmInfo> dto = (PageQueryDTO<UmInfo>) QueryUtil.FltQuery("ge", umInfo, page, umInfoMapper, wrapper);


        return Result.success(dto);
    }

    /**
     * 新增数据
     */
    @Override
    public Result<UmInfo> insert(UmInfo umInfo) {

        if (umInfo.getUminfoId() != null) {
            return Result.error("07011", "请勿添加ID信息");
        }
        boolean save = save(umInfo);
        return save ? Result.success(umInfo) : Result.error("07010", "新增数据失败");
    }

    /**
     * 更新数据
     *
     * @param umInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Result<UmInfo> update(UmInfo umInfo) {
        Integer Id = umInfo.getUmId();
        if (Id == null || getById(Id) == null) {
            return Result.error("07021", "ID不存在");
        }

        boolean update = updateById(umInfo);
        return update ? Result.success(umInfo) : Result.error("07020", "更新数据失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param umInfoId 主键
     * @return 是否成功
     */
    @Override
    public Result<Boolean> deleteById(Integer umInfoId) {
//      暂时不用判空
        boolean remove = removeById(umInfoId);
        return remove ? Result.success(true) : Result.error("07030", "删除数据失败");
    }
}